<?php
class model_event extends model_weixin{
	
	public static $event = '';
    
    
    /**
     * @desc 检测消息事件类型
     * @author Jackie Yang
     */
    public static function checkEvent(){
        if(!empty(self::$postDataObj)){
            self::$event = self::$postDataObj -> Event;
            return self::$event;
        }else{
            echo "";
            exit;
        }
    }
    
    /**
     * @desc 获取自定义菜单点击Key值
     */
    public static function getEventKey() {
    	$event_key = '';
    	if(!empty(self::$postDataObj)) {
    		$event_key = self::$postDataObj -> EventKey;
    	}
    	return $event_key;
    }
    
    /**
     * @desc 关注事件处理
     * @author Jackie Yang
     */
    public static function subscribe(){
        if(!empty(self::$postDataObj)){
            //注册账号
            model_user::resgiter(self::$fromUserName);
            //回复消息
           /* $time = time();
            $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        <FuncFlag>0</FuncFlag>
                        </xml>";             
            $msgType = "text";
            $contentStr = "您好，欢迎关注日照钢铁营销服务中心微信公众服务号！";
            $resultStr = sprintf($textTpl, self::$fromUserName, self::$toUserName, $time, $msgType, $contentStr);
            framework_static_function::write_log($resultStr,'weixin');
            echo $resultStr;*/
            self::related_companys();
        }else{
            echo '';
        }
    }

    /**
     * @desc 取消关注事件处理
     * @author Jackie Yang
     */
    public static function unsubscribe(){
        if(!empty(self::$postDataObj)){
            //更改数据库状态  取消关注
            $binds['subscribe'] = 0;
            model_user::update_weixin_user_info($binds,self::$fromUserName);
            $time = time();
            $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        <FuncFlag>0</FuncFlag>
                        </xml>";
            $msgType = "text";
            $contentStr = "感谢您的支持！";
            $resultStr = sprintf($textTpl, self::$fromUserName, self::$toUserName, $time, $msgType, $contentStr);
            framework_static_function::write_log($resultStr,'weixin');
            echo $resultStr;
        }else{
            echo '';
        }
    }
    /**
     * 处理带单点击事件
     * @param string $event_key 事件key
     */
    public static function do_menu_click($event_key) {
    	switch ($event_key) {
    		case 'ABOUT_US': //企业简介
    			self::about_us();
    			break;
    		case 'ABOUT_PRODUCTS': //产品介绍
    			self::about_products();
    			break;
    		case 'CHAT': //我要交流
    			self::chat();
    			break;
    		case 'PRODUCT_FEATURES': //产品特性
    			self::product_features();
    			break;
    		case 'CONTACT': //联系我们
    			self::contact();
    			break;
    		default:
    			self::related_companys();//关联企业
    			break;
    	}
    }
    /**
     * 关联企业
     */
    public static function related_companys() {
    	$catId = model_news::get_catid_by_catName('关联企业');
    	$msg = ctrl_news::getNewsList(self::$fromUserName, $catId,1);
    	echo $msg;
    	exit;
    }
    /**
     * 企业简介
     */
    public static function about_us() {
    	$catId = model_news::get_catid_by_catName('企业简介');
    	$msg = ctrl_news::getNewsList(self::$fromUserName, $catId,1);
    	echo $msg;
    	exit;
    }
    /**
     * 产品介绍
     */
    public static function about_products(){
    	$catId = model_news::get_catid_by_catName('产品介绍');
    	$msg = ctrl_news::getNewsList(self::$fromUserName, $catId,10);
    	echo $msg;
    	exit;
    }
    /**
     * 我要交流
     */
    public static function chat() {
    	$catId = model_news::get_catid_by_catName('我要交流');
    	$msg = ctrl_news::get_topic_list(self::$fromUserName, $catId,10);
    	echo $msg;
    	exit;
    }
    /**
     * 产品特性
     */
    public static function product_features(){
    	$catId = model_news::get_catid_by_catName('行情资讯');
    	$msg = ctrl_news::getNewsList(self::$fromUserName, $catId,10);
    	echo $msg;
    	exit;
    }
    /**
     * 联系我们
     */
    public static function contact(){
    	$catId = model_news::get_catid_by_catName('联系我们');
    	$msg = ctrl_news::getNewsList(self::$fromUserName, $catId,10);
    	echo $msg;
    	exit;
    }
}